<%--
  Created by IntelliJ IDEA.
  User: 肖
  Date: 2020/10/4
  Time: 14:56
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<br>
<div class="row" style="margin-top: 10px;">
    <div class="col-md-6">
        <button id="cars" type="button" class="btn btn-primary btn-sm">
            <span class="glyphicon glyphicon-refresh"></span>刷新
        </button>
        <button id="more_del_btn_car"  type="button" class="btn btn-danger btn-sm">
            <span class=" glyphicon glyphicon-trash"></span>批量删除
        </button>

    </div>
    <div class="col-md-6" style="float: right;">
        <form class="bs-example bs-example-form" role="form">
            <div class="row">
                <div class="input-group input-group-sm col-md-6" style="margin: 0 auto;">
                    <input type="text" id="carsearch" class="form-control" placeholder="请输入" value="${search}">
                    <span class="input-group-btn">
                                    <button id="carbutton" class="btn btn-default btn-primary" type="button">
                                        <span class="glyphicon glyphicon-search"></span>&nbsp搜索
                                </button>
                                </span>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="row">
    <div class="tab-pane fade in active">
        <table class="table table-hover table-responsive">
            <thead>
            <tr>
                <td><input id="ck_all" type="checkbox"></td>
                <th>车ID</th>
                <th>车主Name</th>
                <th>车主phone</th>
                <th>创建时间</th>
                <th>底价</th>
                <th>标价</th>
                <th>状态</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <c:forEach items="${page.list}" var="p" varStatus="vs">
                <tr>
                    <th><input value="${p.carId}" name="ck_content" type="checkbox"></th>
                    <td>${p.carId}</td>
                    <td>${p.oname}</td>
                    <td>${p.ophone}</td>
                    <td><fmt:formatDate value='${p.createDate}' pattern='yyyy-MM-dd' /></td>
                    <td ondblclick='update_caBaseprice(this,${p.carId})'>${p.caBaseprice}</td>
                    <td ondblclick='update_caSellprice(this,${p.carId})'>${p.caSellprice}</td>
                    <td ondblclick='update_cStatus(this,${p.carId})'>${p.cStatus}</td>
                    <td>
                        <a onclick='del(this,${p.carId})' class="text-danger" href="#"><span class="glyphicon glyphicon-trash"></span></a>
<%--                                                <a class="text-warning" href="#"><span class="glyphicon glyphicon-edit"></span></a>--%>
                    </td>
            </c:forEach>
            </tbody>
        </table>
    </div>
</div>

<script>

    //批量删除start
    $(function(){
        let ck = document.getElementById("ck_all");
        let cks = document.getElementsByName("ck_content");
        ck.onclick=function(){
            if(ck.checked){
                for(let i=0;i<cks.length;i++){
                    cks[i].checked=true;
                }
            }else{
                for(let i=0;i<cks.length;i++){
                    cks[i].checked=false;
                }
            }
        }
    });
    $(function(){
        document.getElementById("more_del_btn_car").onclick=function(){
            let cks = document.getElementsByName("ck_content");
            let count = 0;
            for(let i=0;i<cks.length;i++){
                if(cks[i].checked){
                    count++;
                }
            }
            if(count==0){
                alert("请先选中!");
                return;
            }
            let flag = confirm("确定删除吗?");
            if(flag){
                let sids = "";
                for(let i=0;i<cks.length;i++){
                    if(cks[i].checked){
                        sids+=cks[i].value+":";
                    }
                }
                $.post('/prj/seller/delallTcar',{"sid":sids},function(data){
                    if(data.code=="200"){
                        $("#tcar").load("/prj/seller/tcar",{id:4});
                    }
                })

            }
        };
    });



    //异步刷新
    $("#cars").click(function () {

        $("#tcar").load("/prj/seller/tcar",{id:selid});
    })
//全局搜索
    $("#carbutton").click(function () {
        let search = $("#carsearch").val();
        $("#tcar").load("/prj/seller/tcar",{id:selid,search:search});
    })



    function update_caBaseprice(obj,id){
        let str = "<input onblur='update_input_caBaseprice(this,"+id+")' type='text' value='"+obj.innerText+"'>";
        obj.innerHTML = str;
    }
    function update_input_caBaseprice(obj,id){
        $.post('/prj/seller/update',{"cPrice":obj.value,"id":id},function(data){
            if(data.code=="200"){
                obj.parentElement.innerText=obj.value;
            }
        })
    }

    function update_caSellprice(obj,id){
        let str = "<input onblur='update_input_caSellprice(this,"+id+")' type='text' value='"+obj.innerText+"'>";
        obj.innerHTML = str;
    }
    function update_input_caSellprice(obj,id){
        $.post('/prj/seller/update',{"cOprice":obj.value,"id":id},function(data){
            if(data.code=="200"){
                obj.parentElement.innerText=obj.value;
            }
        })
    }

    function update_cStatus(obj,id){
        let str = "<input onblur='update_input_cStatus(this,"+id+")' type='text' value='"+obj.innerText+"'>";
        obj.innerHTML = str;
    }
    function update_input_cStatus(obj,id){
        $.post('/prj/seller/update',{"cStatus":obj.value,"id":id},function(data){
            if(data.code=="200"){
                obj.parentElement.innerText=obj.value;
            }
        })
    }

//异步删除
    function del(obj,id){
        $.post('/prj/seller/tcardel',{"id":id},function(data){
            if(data.code=="200"){

                let tr = obj.parentElement.parentElement;
                $(tr).remove();
            }else {
                alert("删除失败")
            }
        })
    }

</script>
<%@include file="../page/tcarpage.jsp" %>